
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>EOS 子弹消息</title>
    <meta id="i18n_pagename" content="message">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="stylesheet" type="text/css" href="https://static.medishares.net/css/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="https://static.medishares.net/css/common.css?v=2018082901">
    <script src="js/jquery-3.2.1.min.js"></script>
    <script src="https://static.medishares.net/js/vue.min.js"></script>
    <script src="https://static.medishares.net/js/common.js?v=2018082901"></script>
</head>
<body>
<div class="mds-group">
    <link href="https://static.medishares.net/css/market/qixi2018.css" rel="stylesheet">
<div class="qixi">
	<div class="card">
    <textarea id="msgcontent" maxlength="256" class="protocol" placeholder="消息内容" onchange="this.value=this.value.substring(0, 256)" onkeydown="this.value=this.value.substring(0, 256)"></textarea>
    <span class="number"><span class="written"></span><span class="slash"> / </span>256</span><br>
    <textarea id="msgrecv" class="protocol" placeholder="所有接收消息的账户名，用回车分隔"></textarea>
		<div style="margin-bottom: 20px;">
			<div class="right">0.0001 EOS/条</div>
		</div>
		<a href="javascript:;" class="confirm">发送消息</a>
	</div>
	<div style="clear: both;"></div>
	<ul class="qixi-about">
		<li>
			<h3>关于 EOS 子弹消息</h3>
			<div>群发消息给好友，同时每人送 0.0001 EOS。</div>
		</li>
		<li>
			<h3>应用场景</h3>
			<ul class="application">
				<li>群发祝福</li>
				<li>商业推广</li>
				<li>就是想发柚子…</li>
			</ul>
		</li>
	</ul>
</div>

<!-- loading -->
<div id="app_loading" v-bind:class="{hide:!loading}"></div>
<!-- 额外js -->
<script src='https://static.medishares.net/js/plugins/lottie_light.min.js'></script>
<script type="text/javascript">
  $(function(){
    lottie.loadAnimation({
      container: $('#app_loading')[0], // the dom element that will contain the animation
      renderer: 'svg',
      loop: true,
      autoplay: true,
      path: 'https://static.medishares.net/js/plugins/loading.json' // the path to the animation json
    });
  })
</script>

<!-- import eosjs & mds-eosjs plugin -->
<script src="js/eos.min.js"></script>
<script src="js/jquery.mdseos.js"></script>

<script type="text/javascript">

	var pageData = {
		// pageInit : true,
	  // loading : true
    loading : false
	}

	var vote1Vue = new Vue({
	  el: '.mds-group',
	  data: pageData,
	})

	$(function(){

    $.mdseos.init(
      {"nodes":[
        {"jsonRpc":"https:\/\/jungle.eosio.cr","chainID":"038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca"}, // 0: jungle-testnet-node
        {"jsonRpc":"https:\/\/mainnet.eoscannon.io","chainID":"aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906"}  // 1: mainnet-node, https may needed
      ]});

  	$.mdseos.app_get_account(function(){
  		pageData.loading = false;
  	});

		// 发起交易
		$('.confirm').on('click',function(){
			$.mdseos.app_get_account(function(){
	  		account = $.mdseos.getAccount();
	  		permission = $.mdseos.getAccountPermission();

	  	  var mdseos = $.mdseos.getEos();

	  	  var comment = $('#msgcontent').val();
	  	  if( computeByteSize(comment) > 256 ){
	  	  	alert('最多只能写256个字节哦~');
	  	  	return false;
	  	  }

        var actionsArr = new Array();

        var recvs = $('#msgrecv').val();
        var recvarr= new Array(); //定义一数组
        recvarr=recvs.split("\n"); //字符分割
        for (i=0;i<recvarr.length ;i++ )
        {
          if (recvarr[i] != ''){
            tmpdata = '{"account": "eosio.token","name": "transfer","authorization": [{"actor": "' +account+ '","permission": "' +permission+ '"}],"data": {"from": "' +account+ '","to": "' +recvarr[i]+ '","quantity": "0.0001 EOS","memo": "' +comment+ '"}}';
            // console.log(tmpdata);
            actionsArr.push(JSON.parse(tmpdata));
          }
        }

        // alert(actionsArr);

	  	  //发起购买交易
	  	  var transaction = mdseos.transaction({
	  	    actions:actionsArr
	  	  },function(error, result){
	  	    if(error){
	  	      alert( JSON.parse(error).error.details[0].message );
	  	    }else{
	  	      alert('发送成功！');
	  	      window.location.reload();
	  	    }
	  	  });

	  	 });
    });


		// 页面相关

		$("#msgcontent").bind('input propertychange',function(e){
			var str = $(this).val(),len = computeByteSize(str);
			$('.written').html(len)
			$('.written').show();
			$('.slash').show();
			if(len>256){
				$('.written').addClass('warn');
			}else{
				$('.written').removeClass('warn')
			}
  	})
		// 计算指定字符串的字节数
		function computeByteSize(str){
		  for (var i=0,byte=0; i < str.length; i++) {
		  	var charCode = str.charCodeAt(i);
		    if (0 <= charCode && charCode <= 0x7f) {
			    byte += 1;
			  } else if (128 <= charCode && charCode <= 0x7ff) {
			    byte += 2;
			  } else if (2048 <= charCode && charCode <= 0xffff) {
			    byte += 3;
			  } else if (65536 < charCode && charCode <= 0x1FFFFF) {
			    byte += 4;
			  } else if (0x200000 < charCode && charCode <= 0x3FFFFFF) {
			    byte += 5;
			  } else if (0x4000000 < charCode && charCode <= 0x7FFFFFFF) {
			    byte += 6;
			  }
		  }
		  return byte
		}

	})
</script>

</div>
</body>
</html>
